﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

using Audit;

namespace СorporativeSystem
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Init();
        }

        public DistributionsParameters DistributionsParameters;

        private void Init()
        {
            DistributionsParameters = new DistributionsParameters();

            grid.Children.Clear();
            grid.RowDefinitions.Clear();
            grid.ColumnDefinitions.Clear();

            grid.ColumnDefinitions.Add(new ColumnDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());

            var currentRow = 0;
            foreach (var distribution in DistributionsParameters.Distributions.Values)
            {

                grid.RowDefinitions.Add(new RowDefinition());

                var txtDistribution = new TextBlock { Text = distribution.Name, Margin = new Thickness(2), VerticalAlignment = VerticalAlignment.Center };
                Grid.SetRow(txtDistribution, currentRow);
                Grid.SetColumn(txtDistribution, 0);
                Grid.SetColumnSpan(txtDistribution, 2);
                grid.Children.Add(txtDistribution);

                currentRow++;

                foreach (var parameter in distribution.Parameters)
                {
                    grid.RowDefinitions.Add(new RowDefinition());

                    var lblParameter = new TextBlock { Text = parameter.Name, Margin = new Thickness(2), VerticalAlignment = VerticalAlignment.Center };
                    Grid.SetRow(lblParameter, currentRow);
                    Grid.SetColumn(lblParameter, 0);
                    grid.Children.Add(lblParameter);

                    var lblValidation = new TextBlock { Foreground = Brushes.Red, Margin = new Thickness(2), VerticalAlignment = VerticalAlignment.Center };
                    Grid.SetRow(lblValidation, currentRow);
                    Grid.SetColumn(lblValidation, 2);
                    grid.Children.Add(lblValidation);

                    var txtParameter = new TextBox { Text = parameter.Value.ToString(), Tag = new GridItem() { Parameter = parameter, TxtValidation = lblValidation }, Margin = new Thickness(2) };
                    Grid.SetRow(txtParameter, currentRow);
                    Grid.SetColumn(txtParameter, 1);
                    grid.Children.Add(txtParameter);

                    currentRow++;
                }
            }
        }

        private bool ResetDistributionsParameters()
        {
            var isValid = true;
            foreach (var child in grid.Children)
            {
                var txt = child as TextBox;
                if (txt != null)
                {
                    var gridItem = txt.Tag as GridItem;
                    if (gridItem != null)
                    {
                        if (!ValidDouble(txt.Text, gridItem))
                        {
                            isValid = false;
                        }
                    }
                }
            }

            return isValid;
        }

        private bool ValidDouble(string text, GridItem gridItem)
        {
            double value;

            if (double.TryParse(text, out value))
            {
                gridItem.Parameter.Value = value;
                gridItem.TxtValidation.Text = string.Empty;
                return true;
            }

            gridItem.TxtValidation.Text = "Неверный формат. Укажите число.";
            return false;
        }


        private void Modelling_Click(object sender, RoutedEventArgs e)
        {
            if (ResetDistributionsParameters())
            {
                var auditImitation = new AuditImitation.AuditImitation();
                var graphicsInfo = auditImitation.StartImitation(DistributionsParameters);

                var ordersGraphicWindow = new OrdersGraphicWindow(graphicsInfo);
                ordersGraphicWindow.Show();

                var materialGraphicWindow = new MaterialGraphicWindow(graphicsInfo);
                materialGraphicWindow.Show();

                var delaysGraphicWindow = new DelaysGraphicWindow(graphicsInfo);
                delaysGraphicWindow.Show();
            }
        }
    }
}
